package com.chaodev.base.controller;

import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.ActivityDivisionDto;
import com.chaodev.base.model.*;
import com.chaodev.base.service.ActivityDivisionService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("articleDivision")
@Api(tags = "活动分工")
public class ActivityDivisionController {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private ActivityDivisionService activityDivisionService;

    @PostMapping(value = "/addActivityDivision")
    @ApiOperation(value = "新增活动分工")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result addActivityDivision(@RequestBody ActivityDivision activityDivision, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (activityDivision.getActivityId() == null || "".equals(activityDivision.getActivityId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少活动id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            activityDivision.setCreateUser(userInfo.getUserId());
            if (activityDivisionService.addActivityDivision(activityDivision)) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            } else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(activityDivision.toString(), "新增活动分工", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/editActivityDivision")
    @ApiOperation(value = "修改活动分工")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result editActivityDivision(@RequestBody ActivityDivision activityDivision, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (activityDivision.getId() == null || "".equals(activityDivision.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            activityDivision.setUpdateUser(userInfo.getUserId());
            if (activityDivisionService.editActivityDivision(activityDivision)) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            } else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(activityDivision.toString(), "修改活动分工", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/removeActivityDivision")
    @ApiOperation(value = "删除活动分工")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result removeActivityDivision(@RequestBody ActivityDivision activityDivision, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (activityDivision.getId() == null || "".equals(activityDivision.getId())) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少id");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if (activityDivisionService.removeActivityDivision(activityDivision)) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            } else {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("操作失败");
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(activityDivision.toString(), "删除活动分工", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getActivityDivisionList")
    @ApiOperation(value = "查询活动分工列表")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getActivityDivisionList(@RequestBody ActivityDivisionDto activityDivisionDto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            result.setData(activityDivisionService.getActivityDivisionList(activityDivisionDto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(activityDivisionDto.toString(), "查询活动分工列表", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getActivityDivisionPage")
    @ApiOperation(value = "查询活动分工分页")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getActivityDivisionPage(@RequestBody ActivityDivisionDto activityDivisionDto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (activityDivisionDto.getPage() == null || activityDivisionDto.getPage() == 0
                    || activityDivisionDto.getRows() == null || activityDivisionDto.getRows() == 0) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少页码或条数");
                return result;
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            result.setData(activityDivisionService.getActivityDivisionPage(activityDivisionDto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(activityDivisionDto.toString(), "查询活动分工分页", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @GetMapping(value = "/getVolunteerByActivity")
    @ApiOperation(value = "根据活动获取报名志愿者")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getVolunteerByActivity(@RequestParam String id, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows, HttpServletRequest request) {
        Result result = Result.success(null, "活动报名志愿者获取成功");
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if (null == page || null == rows) {
                result.setData(activityDivisionService.getVolunteerByActivity(id));
            } else {
                PageInfo<Volunteer> volunteerPage = activityDivisionService.getVolunteerByActivity(id, page, rows);
                Map<String, Object> resultMap = new HashMap<>(2);
                resultMap.put("list", volunteerPage.getList());
                resultMap.put("total", volunteerPage.getTotal());
                result.setData(resultMap);
            }
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(id, "根据活动获取报名志愿者", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getLoginNotDivisionPage")
    @ApiOperation(value = "获取当前人待分工活动分页列表")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getLoginNotDivisionPage(@RequestBody ActivityDivisionDto activityDivisionDto, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (!redisDao.existsKey(token)) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("登录超时");
                return result;
            }
            if (activityDivisionDto.getPage() == null || activityDivisionDto.getPage() == 0
                    || activityDivisionDto.getRows() == null || activityDivisionDto.getRows() == 0) {
                result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                result.setMsg("缺少页码或条数");
                return result;
            }

            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            activityDivisionDto.setVolunteerUserCode(userInfo.getUserId());
            result.setData(activityDivisionService.getActivityNoticeNotDivisionPage(activityDivisionDto));
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("查询成功");
        } catch (Exception exception) {
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(activityDivisionDto.toString(), "获取当前人待分工活动分页列表", resultCode, logResult.toString(), userInfo, request);
        }
        return result;
    }

}
